<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Media Manager - ISBN page</title>
<link rel="stylesheet" type="text/css" href="media.css" />
<link rel="shortcut icon" href="../favicon.ico" />
<script type='text/javascript' src='http://www.quotationaday.com/jquery/jquery.js'></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p.toggle").toggle("slow");
  });
});
</script>


</head>
<body>

<h1>Mark's Media Manager</h1>
<h2>Current Version: 0.55 (August 31st, 2014) r9</h2>
<!--<a target='_blank' href='report.php'>Report</a>-->
<br/><br/>
<button>Click for instructions</button>
<!--
<p class="toggle" style="display: none">
Type in as much or as little information as you like - we will try to fill in the rest for you.<br/>
We suggest entering the type, as well as the ISBN or the title + author.<br/>
Please note that we cannot read your mind (until version 1.6) so we don't know where or when you bought your item, how much you paid for it or whether it was used. If you want these fields in your media list you'll have to enter them for each item.<br/>
Author - enter up to three authors or composers for your item<br/>
</p>
-->
<p class="toggle" style="display: none">
Enter the title of the book you are trying to find out about, and the rest of the information will be displayed. If the information displayed is not correct, click the link to enter a bug report. Alternatively, to send the admin an automatic email with the search details, click the button.  
</p>
<br/><br/>
<?php
//query block
require 'connect.php';

//set up user agent
include 'ua.php';
$ua=getBrowser();
$browser = $ua['name'];

if ($browser == 'Apple Safari')
{
$instruction = "For Safari, select a value from the dropdown or enter a new value in the text box<br/>";
}

//queries
date_default_timezone_set('America/Denver');//set timezone
$sqlGetAuthor = "SELECT author_id, author1 FROM author ORDER BY author1";
$sqlGetCurrency = "SELECT currency_id, currency FROM currency ORDER BY 2";
$sqlGetPublisher = "SELECT publisher_id, publisher FROM publisher ORDER BY 2";
$sqlGetLanguage = "SELECT language_id, language FROM language ORDER BY language";
$sqlGetVendor = "SELECT vendor_id, vendor FROM vendor ORDER BY vendor";
$sqlGetUser = "SELECT media_user_id, username FROM media_user where media_user_id >1 ORDER BY username";

//fill results
try{
$STH = $DBH->prepare($sqlGetAuthor);
$STH->execute();
$sqlAuthorResult = $STH->fetchAll();

$STH = $DBH->prepare($sqlGetCurrency);
$STH->execute();
$sqlCurrencyResult = $STH->fetchAll();

$STH = $DBH->prepare($sqlGetPublisher);
$STH->execute();
$sqlPublisherResult = $STH->fetchAll();

$STH = $DBH->prepare($sqlGetLanguage);
$STH->execute();
$sqlLanguageResult = $STH->fetchAll();

$STH = $DBH->prepare($sqlGetVendor);
$STH->execute();
$sqlVendorResult = $STH->fetchAll();

$STH = $DBH->prepare($sqlGetUser);
$STH->execute();
$sqlUserResult = $STH->fetchAll();

}
catch(PDOException $e) {
	echo $e->getMessage();
}

echo '<div class="container"><form name="media" method="post" autocomplete="off">';//create a container

echo "<br/>User:<select name='user'>";
foreach ($sqlUserResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[0])."'>".stripslashes($row[1])."</option><br/>";	
	}
echo "</select>";

//title
echo "<br/>Title:<input type='text' name='title' required/>";
//type
echo "<br/>Type<select name='type'>";
echo "<option value='' selected='selected'></option>";
echo "<option value='Book'>Book</option>";
echo "<option value='CD'>Music-CD</option>";
echo "<option value='Vinyl'>Music-Vinyl</option>";
echo "<option value='Tape'>Music-Tape</option>";
echo "<option value='DVD'>Movie-DVD</option>";
echo "<option value='Blu Ray'>Movie-Blu Ray</option>";
echo "<option value='VCR'>Movie-VCR</option>";
echo "</select>";

echo "<script>
$('select[name=type]').change(function(e){
  if ($('select[name=type]').val() == 'Book'){
    $('#book').show();
  }else{
    $('#book').hide();
  }
});
$('select[name=type]').change(function(e){
  if ($('select[name=type]').val() == 'CD'||$('select[name=type]').val() == 'Vinyl'||$('select[name=type]').val() == 'Tape'
  ){
    $('#music').show();
  }else{
    $('#music').hide();
  }
});
$('select[name=type]').change(function(e){
  if ($('select[name=type]').val() == 'DVD'||$('select[name=type]').val() == 'Blu Ray'||$('select[name=type]').val() == 'VCR'
  ){
    $('#movie').show();
  }else{
    $('#movie').hide();
  }
});
</script>";

//books
//show datalist and display a box for each author
echo "<div id='book'>";
echo "<fieldset><legend>Authors:</legend>";
echo $instruction;
echo "Author1: <input type=text list=authors name=author1  size='35' autofocus>";
echo "<datalist id=authors><select>";

foreach ($sqlAuthorResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[0])."'>".stripslashes($row[1])."</option><br/>";	
	}
echo "</select></datalist><br/>";
echo "Author2: <input type=text list=authors name=author2 size='35'>";
echo "<datalist id=authors><select>";

foreach ($sqlAuthorResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[0])."'>".stripslashes($row[1])."</option><br/>";		
	}
echo "</select></datalist><br/>";
echo "Author3: <input type=text list=authors name=author3 size='35'>";
echo "<datalist id=authors><select>";

foreach ($sqlAuthorResult as $row)
	{
echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[0])."'>".stripslashes($row[1])."</option><br/>";	
	}
echo "</select></datalist><br/>";
echo "</fieldset>";

//publisher
echo "Publisher: <br/>";
echo $instruction;
echo "<input type=text list=publisher name=publisher>";
echo "<datalist id=publisher><select>";

foreach ($sqlPublisherResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[1])."'>".stripslashes($row[1])."</option><br/>";		
	}
echo "</select></datalist><br/>";

//genre??

echo "</div>";

//movies
echo "<div id='movie'>";
echo $instruction;
echo "Director: <input type=text list=directors name=director size='35'>";
echo "<datalist id=directors><select>";

foreach ($sqlAuthorResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[1])."'>".stripslashes($row[1])."</option><br/>";		
	}
echo "</select></datalist><br/>";
echo "Director2: <input type=text list=directors name=director2 size='35'>";
echo "<datalist id=directors><select>";

foreach ($sqlAuthorResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[1])."'>".stripslashes($row[1])."</option><br/>";		
	}
echo "</select></datalist><br/>";
echo "</div>";

//music
echo "<div id='music'>";
echo "<fieldset><legend>Composers:</legend>";
echo $instruction;
echo "Composer1: <input type=text list=composers name=composer1  size='35' autofocus>";
echo "<datalist id=composers><select>";

foreach ($sqlAuthorResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[1])."'>".stripslashes($row[1])."</option><br/>";		
	}
echo "</select></datalist><br/>";
echo "Composer2: <input type=text list=composers name=composer2 size='35'>";
echo "<datalist id=composers><select>";

foreach ($sqlAuthorResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[1])."'>".stripslashes($row[1])."</option><br/>";		
	}
echo "</select></datalist><br/>";
echo "Composer3: <input type=text list=composers name=composer3 size='35'>";
echo "<datalist id=composers><select>";

foreach ($sqlAuthorResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[1])."'>".stripslashes($row[1])."</option><br/>";		
	}
echo "</select></datalist><br/>";
echo "</fieldset>";
echo "Label:<br/>";
echo $instruction;
echo "<input type=text list=labels name=label size='35'>";
echo "<datalist id=labels><select>";

foreach ($sqlAuthorResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[1])."'>".stripslashes($row[1])."</option><br/>";		
	}
echo "</select></datalist><br/>";
echo "</div>";

//language
echo "<br/>Language:<br/>";
echo $instruction;
echo "<input type=text list=language name=language>";
echo "<datalist id=language><select>";

foreach ($sqlLanguageResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[1])."'>".stripslashes($row[1])."</option><br/>";	
	}
echo "</select></datalist><br/>";

//cost
echo "<br/>Cost: <input type='number' name='cost' min='0' maxlength='10' step='any'/><br/>";
//paid
//echo "Paid <input type='number' name='paid'/><br/>";//replace this with the current amazon price 

//currency
echo "Currency:<br/>";
echo $instruction;
echo "<input type=text list=currency name=currency>";
echo "<datalist id=currency><select>";

foreach ($sqlCurrencyResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[1])."'>".stripslashes($row[1])."</option><br/>";		
	}
echo "</select></datalist><br/>";

//purchase date
$currentDate = date('Y-m-d');
echo "Purchase Date: <input type='date' name='purchasedate' value='$currentDate'/><br/>";

//purchase place (vendor)
echo "Vendor:<br/>";
echo $instruction;
echo "<input type=text list=vendor name=vendor>";
echo "<datalist id=vendor><select>";

foreach ($sqlVendorResult as $row)
	{
	echo "<option label='".stripslashes($row[1])."' value='".stripslashes($row[1])."'>".stripslashes($row[1])."</option><br/>";		
	}
echo "</select></datalist><br/>";

//used flag
echo "Used: <input type=checkbox name='used' value='1'/><br/>";

//year
$currentYear = date('Y');
echo "<br/>Release Year (for BC enter negative numbers):<input type='number' name='year' max='$currentYear' placeholder='$currentYear'/><br/>";

echo "ISBN/SSN/etc.:<input type='text' name='ISBN'/><br/>";
echo "Comments: <textarea name='comments' rows='4' cols='69' placeholder='enter any comments here'></textarea>";
echo '<br/><input type="submit" value="Submit"/>';
echo '<input type=hidden name="submitted" value="1"/>';
echo '</form></div>';


if ($_POST[submitted]==1)
//code to be executed if form is submitted
{
//assign post values to variables
$user = $_POST[user];
$author1 = $_POST[author1];
$authorLength = strlen($author1);
$author2 = $_POST[author2];
$author3 = $_POST[author3];

$language = $_POST[language];
$languageLength = strlen($language);

$cost = $_POST[cost];
$costLength = strlen($language);

$type = $_POST[type];
$typeLength = strlen($type);
if ($typeLength < 1)
{
$type = 'Book';
}

$purchaseDate = $_POST[purchasedate];
$used = $_POST[used];
$vendor = $_POST[vendor];
$comments = $_POST[comments];
//$paid = $_POST[paid];
$currency = $_POST[currency];

$publisher = $_POST[publisher];
$publisherLength = strlen($publisher);

$title = $_POST[title];
$titleLength = strlen($title);

$year  = $_POST[year];
$yearLength = strlen($year);

$ISBN = $_POST[ISBN];
$ISBNLength = strlen($ISBN);

//check ISBN length
if (strlen($ISBN)>0)
{
	if (strlen($ISBN)==10)
	{
		if (is_isbn_10_valid($ISBN)==1)
		{}
		else
		{
		echo "$ISBN Invalid";
		exit;
		}
		
	}
	else if (strlen($ISBN)==13)
	{
		if (is_isbn_13_valid($ISBN)==1)
		{}
		else
		{
		echo "$ISBN Invalid";
		exit;
		}
	}
	else
	{
	echo "ISBN length invalid";
	exit;
	}
} 

//API section 
//if title is working
if (isset($title))
{
$rootTitle = $title;
$wiki = 1;
require 'wikijson.php';

if ($type == 'Book')
{
require 'goodreads.php';//only do goodreads search for books
$goodreads = 1;
}

}

include 'tablebuild.php';



//display back information
echo "<hr/><br/>Original form information:<br/>";
echo "Title: $title, $year<br/>";
echo "Authors: $author1"," ","$author2"," ","$author3<br/>";
echo "Language: $language<br/>";
echo "Price: $cost $currency<br/>";
echo "Purchase Date: $purchaseDate<br/>";
echo "Used: $used<br/>";
echo "Vendor: $vendor<br/>";
echo "Publisher: $publisher<br/>";
echo "Type: $type<br/>";
echo "ISBN: <a target=_blank href='https://www.google.com/search?q=$ISBN&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl'>$ISBN</a><br/>";
echo "Comments: $comments<br/>";

$body = "Title: $title, $year<br/>"."Authors: $author1, $author2, $author3<br/>";
$body .= "Language: $language<br/>Price: $cost $currency<br/>Purchase Date: $purchaseDate<br/>";
$body .= "Used: $used<br/>Vendor: $vendor<br/>Publisher: $publisher<br/>Type: $type<br/>";
$body .= "ISBN: <a target=_blank href='https://www.google.com/search?q=$ISBN&btnG=Search+Books&tbm=bks&tbo=1&gws_rd=ssl'>$ISBN</a><br/>";
$body .= "Comments: $comments<br/>";
$body .= $completeurl;

echo '<br/><a href="https://code.google.com/p/marks-media-library/issues/entry" target="_blank">Click here to enter a bug report</a><br/>';
echo '<form name="email" method="post" action="bugmail.php" autocomplete="on">';
echo '<input type=hidden name="body" value="'.$body.'"/>';
echo '<br/><input type="submit" value="Send bug report"/>';
echo '<input type=hidden name="mailsubmitted" value="1"/>';

echo '</form>';

//fill in missing information
//determine list of fields to look up
/*
$authorLength
$languageLength
$costLength
$typeLength
$publisherLength
$titleLength
$yearLength
$ISBNLength
*/


//insert authors
//$sqlInsertAuthor = "INSERT INTO author (author1,author2,author3,datestamp) VALUES ('$author1', '$author2','$author3', (SELECT NOW()))";
//$sqlSelectAuthor = "SELECT author_id FROM author WHERE author1='$author1' AND author2='$author2' AND author3='$author3'";

//take value for author and insert it
//insertQueries($sqlSelectAuthor,$sqlInsertAuthor);

//get author id
//$authorId = getId($sqlSelectAuthor);
//echo "Author id is $authorId<br/>";

//insert currency
//$sqlSelectCurrency = "SELECT currency_id FROM currency WHERE currency='$currency'";
//$sqlInsertCurrency = "INSERT INTO currency (currency, datestamp) VALUES ('$currency',(SELECT NOW()))";
//insertQueries($sqlSelectCurrency,$sqlInsertCurrency);

//get currency id
//$currencyId = getId($sqlSelectCurrency);
//echo "Currency id is $currencyId<br/>";

//insert cost
//$sqlInsertCost = "INSERT INTO cost (cost, paid, currency_id, datestamp) VALUES ('$cost', '$paid', '$currencyId',(SELECT NOW()))";
//mysqli_query($mysqli,$sqlInsertCost);
//$costId = mysqli_insert_id($mysqli);
//echo "Cost id is $costId<br/>";

//insert publisher information
//$sqlSelectPublisher = "SELECT publisher_id FROM publisher WHERE publisher='$publisher'";
//$sqlInsertPublisher = "INSERT INTO publisher (publisher, datestamp) VALUES ('$publisher',(SELECT NOW()))";
//insertQueries($sqlSelectPublisher,$sqlInsertPublisher);

//get publisher id
//$publisherId = getId($sqlSelectPublisher);
//echo "Publisher id is $publisherId<br/>";

//insert media record
//$sqlSelectMedia = "SELECT media_id FROM media WHERE publisher_id = '$publisherId' AND author_id = '$authorId' AND year = '$year' AND title='$title' AND isbn='$ISBN'";
//$sqlInsertMedia = "INSERT INTO media (title, author_id, publisher_id, year, ISBN, type, cost_id, datestamp) VALUES ('$title','$authorId','$publisherId','$year','$ISBN','$type','$costId',(SELECT NOW()))";
//insertQueries($sqlSelectMedia,$sqlInsertMedia);

}




?>

</body>
</html>